package com.gelapps.memogel.cards;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.gelapps.memogel.Constants;

public class CardsXMLHandler extends DefaultHandler{

	 Boolean currentElement = false;
	    String currentValue = "";
	    CardsMaster item = null;
	    private ArrayList<CardsMaster> itemsList = new ArrayList<CardsMaster>();
	 
	    public ArrayList<CardsMaster> getItemsList() {
	        return itemsList;
	    }
	 
	    // Called when tag starts 
	     @Override
	    public void startElement(String uri, String localName, String qName,
	            Attributes attributes) throws SAXException {
	 
	        currentElement = true;
	        currentValue = "";
	        if (localName.equals("cc")) {
	            item = new CardsMaster();
	        } 
	 
	    }
	 
	    // Called when tag closing 
	    @Override
	     public void endElement(String uri, String localName, String qName)
	    		    throws SAXException {
	 
	        currentElement = false;
	 
	        /** set value */
	        if (localName.equalsIgnoreCase(Constants.getCardsName()))            item.setName(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsNumber()))     item.setNumber(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsPIN()))        item.setPin(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsCVV()))        item.setCVV(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsPhone()))      item.setPhoneToBlock(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsCircuits()))   item.setCircuits(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsExpiry()))     item.setExpiry(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsBalance()))	 item.setBalance(currentValue);
            else if (localName.equalsIgnoreCase(Constants.getCardHolder()))      item.setHolder(currentValue);
            else if (localName.equalsIgnoreCase(Constants.getCardBillAddress())) item.setBillAddress(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsBank()))       item.setBank(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsWebSite()))    item.setWebSite(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getCardsNote()))       item.setNote(currentValue);
            else if (localName.equalsIgnoreCase(Constants.getCardsElement()))    itemsList.add(item);
	       // System.out.println("endElement: "+currentValue);
	    }
	 
	    // Called to get tag characters 
	    @Override
	    public void characters(char[] ch, int start, int length)
	    throws SAXException {
	 
	        if (currentElement) {
	            currentValue = currentValue +  new String(ch, start, length);
	        }
	 
	    }

}
